<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>完整的PS1控制码 - whowin - 发表我个人原创作品的技术博客</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="whowin" /><meta name="description" content="PS1变量是在 shell 下用于定义提示符的变量，本文列出了可用于该变量的所有的控制代码，仅供参考.
" /><meta name="keywords" content="linux, socket, hugo, dos" />






<meta name="generator" content="Hugo 0.97.3 with theme even" />


<link rel="canonical" href="https://whowin.gitee.io/post/blog/linux/0002-ps1-control-codes/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.e3fea119b1980e848b03dffbeddb11dd0fba483eed0e5f11870fb8e31f145bbd.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="完整的PS1控制码" />
<meta property="og:description" content="PS1变量是在 shell 下用于定义提示符的变量，本文列出了可用于该变量的所有的控制代码，仅供参考." />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://whowin.gitee.io/post/blog/linux/0002-ps1-control-codes/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2022-04-01T14:00:00+08:00" />
<meta property="article:modified_time" content="2022-04-01T14:00:00+08:00" />

<meta itemprop="name" content="完整的PS1控制码">
<meta itemprop="description" content="PS1变量是在 shell 下用于定义提示符的变量，本文列出了可用于该变量的所有的控制代码，仅供参考."><meta itemprop="datePublished" content="2022-04-01T14:00:00+08:00" />
<meta itemprop="dateModified" content="2022-04-01T14:00:00+08:00" />
<meta itemprop="wordCount" content="647">
<meta itemprop="keywords" content="环境变量,PS1," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="完整的PS1控制码"/>
<meta name="twitter:description" content="PS1变量是在 shell 下用于定义提示符的变量，本文列出了可用于该变量的所有的控制代码，仅供参考."/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

  <script async src="/js/busuanzi.pure.mini.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9724909319263152"
     crossorigin="anonymous"></script>


</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">WhoWin</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">文章归档</li>
      </a><a href="/article-categories/categories/">
        <li class="mobile-menu-item">文章分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">文章标签</li>
      </a><a href="/about/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">WhoWin</a>
  
  <div style="position:absolute; left: 80px; top: 75px; color: crimson">
      ———开源和分享是技术发展的源泉和动力；本博客所有文章均为原创
  </div>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">文章归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/article-categories/categories/">文章分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">文章标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">完整的PS1控制码</h1>

      <div class="post-meta">
        <span class="post-time"> 2022-04-01 </span>
        <div class="post-category">
            <a href="/categories/linux/"> Linux </a>
            </div>
        
      </div>
    </header>

    
    <div class="post-content">
      <p>PS1变量是在 shell 下用于定义提示符的变量，本文列出了可用于该变量的所有的控制代码，仅供参考.</p>
<blockquote>
<p>不同于其它文章中给出的常用控制码，以下内容完全转自man手册，应该是比较完整的</p>
</blockquote>
<table>
<thead>
<tr>
<th style="text-align:center">序号</th>
<th style="text-align:center">参数</th>
<th style="text-align:left">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1</td>
<td style="text-align:center"><code>\a</code></td>
<td style="text-align:left"><code>ASCII</code>字符(<code>07</code>)，响铃<code>bell</code></td>
</tr>
<tr>
<td style="text-align:center">2</td>
<td style="text-align:center"><code>\d</code></td>
<td style="text-align:left">代表日期，格式为<code>weekday month date</code>，例如：<code>&quot;Mon Aug 1&quot;</code></td>
</tr>
<tr>
<td style="text-align:center">3</td>
<td style="text-align:center"><code>\D{format}</code></td>
<td style="text-align:left"><code>format</code>会作为参数传给<code>strftime()</code>，返回的结果将插入提示符，大括号是必须的</td>
</tr>
<tr>
<td style="text-align:center">4</td>
<td style="text-align:center"><code>\e</code></td>
<td style="text-align:left"><code>ASCII</code>字符(<code>033</code>)</td>
</tr>
<tr>
<td style="text-align:center">5</td>
<td style="text-align:center"><code>\h</code></td>
<td style="text-align:left">仅取主机的第一个名字</td>
</tr>
<tr>
<td style="text-align:center">6</td>
<td style="text-align:center"><code>\H</code></td>
<td style="text-align:left">完整的主机名称</td>
</tr>
<tr>
<td style="text-align:center">7</td>
<td style="text-align:center"><code>\j</code></td>
<td style="text-align:left"><code>shell</code>当前管理的任务数</td>
</tr>
<tr>
<td style="text-align:center">8</td>
<td style="text-align:center"><code>\l</code></td>
<td style="text-align:left"><code>shell</code>终端设备名的主名(最后一个斜杠后面的部分)</td>
</tr>
<tr>
<td style="text-align:center">9</td>
<td style="text-align:center"><code>\n</code></td>
<td style="text-align:left">换行</td>
</tr>
<tr>
<td style="text-align:center">10</td>
<td style="text-align:center"><code>\r</code></td>
<td style="text-align:left">回车</td>
</tr>
<tr>
<td style="text-align:center">11</td>
<td style="text-align:center"><code>\s</code></td>
<td style="text-align:left"><code>shell</code>的名称，<code>$0</code>是主名(最后一个斜杠后面的部分)</td>
</tr>
<tr>
<td style="text-align:center">12</td>
<td style="text-align:center"><code>\t</code></td>
<td style="text-align:left">显示时间为<code>24</code>小时，格式：<code>HH:MM:SS</code></td>
</tr>
<tr>
<td style="text-align:center">13</td>
<td style="text-align:center"><code>\T</code></td>
<td style="text-align:left">显示时间为<code>12</code>小时，格式：<code>HH:MM:SS</code></td>
</tr>
<tr>
<td style="text-align:center">14</td>
<td style="text-align:center"><code>\@</code></td>
<td style="text-align:left">显示时间为<code>12</code>小时，<code>am/pm</code>格式</td>
</tr>
<tr>
<td style="text-align:center">15</td>
<td style="text-align:center"><code>\A</code></td>
<td style="text-align:left">显示时间为<code>24</code>小时，格式：<code>HH:MM</code></td>
</tr>
<tr>
<td style="text-align:center">16</td>
<td style="text-align:center"><code>\u</code></td>
<td style="text-align:left">当前用户的用户名</td>
</tr>
<tr>
<td style="text-align:center">17</td>
<td style="text-align:center"><code>\v</code></td>
<td style="text-align:left"><code>BASH</code>的版本信息(比如：<code>2.00</code>)</td>
</tr>
<tr>
<td style="text-align:center">18</td>
<td style="text-align:center"><code>\V</code></td>
<td style="text-align:left"><code>bash</code>的发行版本，版本号+补丁级别(比如：<code>2.00.0</code>)</td>
</tr>
<tr>
<td style="text-align:center">19</td>
<td style="text-align:center"><code>\w</code></td>
<td style="text-align:left">完整的工作目录名称，<code>home</code>目录会以 <code>~</code> 代替</td>
</tr>
<tr>
<td style="text-align:center">20</td>
<td style="text-align:center"><code>\W</code></td>
<td style="text-align:left">工作目录的主名(最后一个斜杠后面的部分)</td>
</tr>
<tr>
<td style="text-align:center">21</td>
<td style="text-align:center"><code>\!</code></td>
<td style="text-align:left">输入命令的<code>history</code>编号</td>
</tr>
<tr>
<td style="text-align:center">22</td>
<td style="text-align:center"><code>\#</code></td>
<td style="text-align:left">下达的第几个命令</td>
</tr>
<tr>
<td style="text-align:center">23</td>
<td style="text-align:center"><code>\$</code></td>
<td style="text-align:left">提示字符，如果是<code>root</code>时，提示符为：<code>#</code> ，普通用户则为：<code>$</code></td>
</tr>
<tr>
<td style="text-align:center">24</td>
<td style="text-align:center"><code>\nnn</code></td>
<td style="text-align:left">用八进制数<code>nnn</code>表示的字符</td>
</tr>
<tr>
<td style="text-align:center">25</td>
<td style="text-align:center"><code>\\</code></td>
<td style="text-align:left">反斜杠</td>
</tr>
<tr>
<td style="text-align:center">26</td>
<td style="text-align:center"><code>\[</code></td>
<td style="text-align:left">一串非打印字符的开始符，可用于在提示符中嵌入终端控制字符串</td>
</tr>
<tr>
<td style="text-align:center">27</td>
<td style="text-align:center"><code>\]</code></td>
<td style="text-align:left">一串非打印字符的结束符</td>
</tr>
</tbody>
</table>
<hr>
<p><strong>欢迎访问我的博客：https://whowin.cn</strong></p>
<p><strong>email: <a href="mailto:hengch@163.com">hengch@163.com</a></strong></p>
<p><img src="https://whowin.gitee.io/images/qrcode/sponsor-qrcode.png" alt="donation"></p>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">whowin</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2022-04-01
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F/">环境变量</a>
          <a href="/tags/ps1/">PS1</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/blog/linux/0003-ansi-escape-code/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">ANSI的ESC转义序列</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/blog/dos/0014-install-dos-on-vbox-under-ubuntu/">
            <span class="next-text nav-default">在vitual box虚拟机上安装dos</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  <span id="/post/blog/linux/0002-ps1-control-codes/" class="leancloud_visitors" data-flag-title="完整的PS1控制码">
		<span class="post-meta-item-text">文章阅读量 </span>
		<span class="leancloud-visitors-count">0</span>
		<p></p>
	  </span>
  <div id="vcomments"></div>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'OFCGzCfJRUglzOdzrqMGkbTR-gzGzoHsz',
        appKey: 'v7P29kPAEbsmaavaYPNhGhnF',
        notify:  false ,
        verify:  false ,
        avatar:'mm',
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:hengch@163.com" class="iconfont icon-email" title="email"></a>
  <a href="https://whowin.gitee.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>
<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2022 - 
    2024<span class="heart"><i class="iconfont icon-heart"></i></span><span>whowin</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.64437849d125a2d603b3e71d6de5225d641a32d17168a58106e0b61852079683.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        }
    };
  </script>
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-mml-chtml.js" integrity="sha256-HGLuEfFcsUJGhvB8cQ8nr0gai9EucOOaIxFw7qxmd+w=" crossorigin="anonymous"></script>








</body>
</html>
